<template>
  <div class="tags-view-container" id="guide-tags">
    <router-link
      @contextmenu.prevent="showMenu($event,index)"
      class="tags-view-item"
      :class="isActive(tag) ? 'active' : ''"
      :style="{
        backgroundColor: isActive(tag) ? $store.getters.cssVar.menuBg : '',
        borderColor: isActive(tag) ? $store.getters.cssVar.menuBg : ''
      }"
      v-for="(tag, index) in $store.getters.tagsViewList"
      :key="tag.fullPath"
      :to="{ path: tag.fullPath }"
    >
      {{ tag.title }}
      <el-icon>
        <close
          v-show="!isActive(tag)"
          @click.prevent.stop="onCloseClick(index)"
        />
      </el-icon>
      <context-menu
        :style="menuStyle"
        v-show="visible"
        :index="selectIndex"
      ></context-menu>
    </router-link>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import ContextMenu from './ContextMenu.vue'
const route = useRoute()

/**
 * 是否被选中
 */
const isActive = (tag) => {
  return tag.path === route.path
}

/**
 * 关闭 tag 的点击事件
 */
const store = useStore()
const onCloseClick = (index) => {
  store.commit('removeTagsView', {
    type: 'index',
    index
  })
}

const visible = ref(false)
const selectIndex = ref(0)
const menuStyle = ref({
  top: 0,
  left: 0
})
const showMenu = (e, index) => {
  const { x, y } = e
  menuStyle.value.top = y + 'px'
  menuStyle.value.left = x + 'px'
  selectIndex.value = index
  visible.value = true
}
watch(
  visible,
  () => {
    if (visible.value) {
      document.body.addEventListener('click', clickClose)
    } else {
      document.body.removeEventListener('click', clickClose)
    }
  }
)
const clickClose = () => {
  visible.value = false
}
</script>

<style lang="scss" scoped>
.tags-view-container {
  height: 34px;
  width: 100%;
  background: #fff;
  border-bottom: 1px solid #d8dce5;
  overflow: hidden;
  box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.12), 0 0 3px 0 rgba(0, 0, 0, 0.04);
  .tags-view-item {
    display: inline-flex;
    align-items: center;
    position: relative;
    cursor: pointer;
    height: 26px;
    line-height: 26px;
    border: 1px solid #d8dce5;
    color: #495060;
    background: #fff;
    padding: 0 8px;
    font-size: 12px;
    margin-left: 5px;
    margin-top: 4px;
    &:first-of-type {
      margin-left: 15px;
    }
    &:last-of-type {
      margin-right: 15px;
    }
    .el-icon {
      margin-left: 5px;
    }
    &.active {
      color: #fff;
      &::before {
        content: '';
        background: #fff;
        display: inline-block;
        width: 8px;
        height: 8px;
        border-radius: 50%;
        position: relative;
        margin-right: 4px;
      }
    }
    // close 按钮
    .el-icon-close {
      width: 16px;
      height: 16px;
      line-height: 10px;
      vertical-align: 2px;
      border-radius: 50%;
      text-align: center;
      transition: all 2s cubic-bezier(0.645, 0.045, 0.355, 1);
      transform-origin: 100% 50%;
      &:before {
        transform: scale(0.6);
        display: inline-block;
        vertical-align: -3px;
      }
      &:hover {
        background-color: #b4bccc;
        color: #fff;
      }
    }
  }
}
</style>
